<?php
/*
客户跟进
*/
define('Acache_no','1');
class Con_report extends Acan{
	public $defaultAct='my';
	public $Conread = '报表';
	public $Actread = array('list'=>'所有记录','my'=>'我的记录');
	function __construct($ainfo=null){
		parent::__construct($ainfo=null);
		if(empty($this->id) || $this->id == '*'){$this->id = '0';}
		$this->uid = Afun_uid();
		$this->model = Afun_db()->model(Acan_table_crm_customer);
		$this->mdeal = Afun_db()->model(Acan_table_crm_deal);
		//print_r($this->model);
	}
	//--所有客户
	function Act_list(){
		$this->atitle = '所有交易量';
		return $this->Act_my();
	}
	//--自己客户
	function Act_my(){
		$op = Afunc_option();
		$bilv = array(
			'au'=>$op->get('crm_ratio_au'),
			'ag'=>$op->get('crm_ratio_ag'),
			'aux'=>$op->get('crm_ratio_aux'),
			'agx'=>$op->get('crm_ratio_agx')
		);
		if(empty($this->atitle)){$this->atitle = '我的交易量';}
		$uid = $this->uid;
		if($this->id>1){
			$uid = $this->id;
		}
		$get = $_GET;
		if(empty($get['date'])){
			$get['date'] = Afun_time_date(strtotime(date("Y").'-'.(date("n")-1)),"Y-m");
		}
		$where = " and c.date LIKE '".$get['date']."%'";
		$obj = Afun_acan_new('list');
		$key_name = $this->mdeal->key_name;
		$columnrr['username'] = '员工';
		$columnrr['realname'] = '客户名称';
		$columnrr = array_merge($columnrr,$this->mdeal->comment);//初始栏目
		unset($columnrr['uid']);
		$column = $obj->column($this->mdeal,$columnrr,$_POST['acolumn']);
		unset($column[$this->mdeal->key_name],$column['cid']);
		$column_html = $obj->column_html($this->mdeal,$column,'Aform');
		$column_json = $obj->form_json($this->model);
		//分页函数
		if($this->Aact=='list' && $this->uid==$uid){
			$con->where = "1".$where;
		}else if($this->Aact=='list' && $this->uid!=$uid){
			$con->where = "a.uid=$uid".$where;
		}else{
			$con->where = "a.uid=$this->uid".$where;//只能查看自己的客户
		}
		$con->where .= " and (c.au>0 or c.ag>0 or c.aux>0 or c.agx>0)";//只显示有交易量的客户
		$con->select = "a.realname,b.username,c.*";
		$con->join = "LEFT JOIN `".Acan_table_user."` b ON a.uid=b.uid LEFT JOIN `".Acan_table_crm_deal."` c on a.cid=c.cid";
		$page->theurl = $this->Aactr.'-'.$this->id;
		$page->by = $this->model->key_name;
		$page->page = '20';
		$page->counts = $this->model->count($con);	//统计总数目
		$pages = Afun_apage($page);
		$con->order = "a.$pages->orderby";
		$con->limit = $pages->limit;
		//分页结束
		$list = $this->model->ofindAll($con);
		$column['total'] = '合计手数';
		if(is_array($list)){
			foreach ($list as $key => $val) {
				$shou = 0;
				foreach ($val as $k => $v) {
					if(in_array($k,array_keys($bilv))){
						$shou = $shou + ($v*$bilv[$k]);
					}
				}
				$list[$key]->total = round($shou,2);
				$total['au']	+= round($list[$key]->au*$bilv['au'],2);
				$total['ag']	+= round($list[$key]->ag*$bilv['ag'],2);
				$total['aux']	+= round($list[$key]->aux*$bilv['aux'],2);
				$total['agx']	+= round($list[$key]->agx*$bilv['agx'],2);
				$total['total']	+= round($list[$key]->total,2);
			}
		}
		$total['realname'] = count($list).'个';
		include $this->Atpl($this->Amod.'/my');
	}

}

?>